package com.zmm.study.order.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 900045
 * @description:
 * @name MyRabbitMqConfig
 * @date By 2021-10-19 19:14:31
 */
@Slf4j
@Configuration
public class MyRabbitMqConfig {

	@Bean
	public MessageConverter messageConverter(){
		return new Jackson2JsonMessageConverter();
	}


	
	// 定义一个死信队列
	
	@Bean
	public Queue orderDelayQueue(){
		// 死信队列
		// name:队列的名称 durable:是不是持久化 exclusive:是不是排它的 autoDelete:是不是自动删除的
		//Queue(String name, boolean durable, boolean exclusive, boolean autoDelete)

		/**
		 * 设置死信队列 -- 自定义参数
		 * x-dead-letter-exchange : order-event-exchange
		 * x-dead-letter-routing-key: order.release.order
		 * x-message-ttl: 180000 /毫秒
		 */
		Map<String,Object> arguments = new HashMap<>(3);
		arguments.put("x-dead-letter-exchange","order-event-exchange");
		arguments.put("x-dead-letter-routing-key","order.release.order");
		arguments.put("x-message-ttl",60000);

		Queue queue = new Queue("order.delay.queue", true, false, false,arguments);
		return queue;
	}

	// 定义一个库存死信队列

	@Bean
	public Queue stockDelayQueue(){
		Map<String ,Object> arguments = new HashMap<>(3);
		// 死信路由
		arguments.put("x-dead-letter-exchange","stock-event-exchange");
		// 用的路由键
		arguments.put("x-dead-letter-routing-key","stock.release");
		// 过期时间 单位 毫秒 120000 = 2分钟
		arguments.put("x-message-ttl",120000);
		return new Queue("stock.delay.queue",true,false,false,arguments);
	}

	// 定义一个普通队列

	@Bean
	public Queue orderReleaseOrderQueue(){
		// 普通的队列
		Queue queue = new Queue("order.release.order.queue", true, false, false);
		return queue;
	}


	// 解锁库存的队列

	@Bean
	public Queue stockReleaseStockQueue(){
		//String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
		return new Queue("stock.release.stock.queue",true,false,false);
	}

	// 定义一个 订单 交换机
	
	@Bean
	public Exchange orderEventExchange(){

		// name: 交换机的名称 durable:是否持久化 autoDelete: 是否自动删除 arguments: 自定义参数
		//String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
		return new TopicExchange("order-event-exchange",true,false);
	}


	// 定义一个 库存 交换机
	
	@Bean
	public Exchange stockEventExchange(){
		//String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
		return new TopicExchange("stock-event-exchange",true,false);
	}
	
	// 定义绑定创建订单关系 
	
	@Bean
	public Binding orderCreateOrderBinding(){
		// destination: 目的地 destinationType: 目的地的类型 exchange: 交换机 routingKey: 路由键 arguments: 自定义参数
		//String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
		return new Binding("order.delay.queue",
				Binding.DestinationType.QUEUE,
				"order-event-exchange",
				"order.create.order",
				null);
	}


	// 定义绑定释放订单关系
	
	@Bean
	public Binding orderReleaseOrderBinding(){
		return new Binding("order.release.order.queue",
				Binding.DestinationType.QUEUE,
				"order-event-exchange",
				"order.release.order",
				null);
	}

	@Bean
	public Binding orderReleaseOtherBinding(){
		return new Binding("stock.release.stock.queue",
				Binding.DestinationType.QUEUE,
				"order-event-exchange",
				"order.release.other.#",
				null);
	}


	// 定义绑定释放库存关系
	
	@Bean
	public Binding stockReleaseBinding(){
		//String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
		return new Binding("stock.release.stock.queue",
				Binding.DestinationType.QUEUE,
				"stock-event-exchange",
				"stock.release.#",
				null);
	}

	// 绑定库存的死信队列

	@Bean
	public Binding stockLockedBinding(){
		//String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
		return new Binding("stock.delay.queue",
				Binding.DestinationType.QUEUE,
				"stock-event-exchange",
				"stock.locked",
				null);
	}
}
